
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;


/** Description of TeamProject Table class
 *
 * @author Dan Woodson
 * @author Trevor Powis
 * @author Kristi Brockelsby
 * @author Jared Gailey
 * @version 1: October 26, 2009
 */
public class TeamProject extends Applet implements ActionListener, MouseListener
{
	char[][] charValues = new char[10][10];	    //array of values
	Table aTable;				    //used to create and store values
	boolean allowUserInput = false;		    // boolean for allowing user input
	Button BtnStart;			    // button used to create 5x5 table
	Button randomChangeBtn;			    // button used to change a value in table
	Button enterCordBtn;			    // button used to submit cordinants
	boolean guessMode;			    // mode allowing the user to guess changed value
	private AudioClip[] sounds = new AudioClip[4];    //array to hold audio clips
	private int counter = 0;		    //counter for audio clip array
	String instructions;			    //opening instructions
	TextField x_txt_field;
	TextField y_txt_field;


	public void init()
	{
    //setup layout
    this.setLayout(new BorderLayout());

	  //setup buttons

		//create button to submit coordinates
		enterCordBtn = new Button("Enter Cordinates");
		enterCordBtn.addActionListener((ActionListener)this);

		//create panel for textfields and enterCordBtn
		Panel cordTxtFlds = new Panel();
    FlowLayout cordTxtFldsLayout = new FlowLayout();
    cordTxtFlds.setLayout(cordTxtFldsLayout);
    cordTxtFlds.setVisible(true);
    x_txt_field = new TextField(2);
    y_txt_field = new TextField(2);

    //add textfields and button
		cordTxtFlds.add(x_txt_field);
		cordTxtFlds.add(y_txt_field);
		cordTxtFlds.add(enterCordBtn);

		//add image to Image objects
		Image fly_img = getImage(getCodeBase(), "images/FLY.gif");

    //setup table
		aTable = new Table(50, 50, 55, 35, 10, 10, fly_img);

    //add cordTxtFlds to bottom of screen
    this.add(cordTxtFlds, BorderLayout.SOUTH);

		super.resize(1000, 1000);
		validate();

		//add mouse listener
		addMouseListener(this);

		//initially guessMode will be false
		guessMode = false;

		//initialize array of audio clips
		sounds[0] = getAudioClip (getCodeBase(), "images/buzzthruloud.wav");	//wrong
		sounds[1] = getAudioClip (getCodeBase(), "images/foghorn.wav");		//wrong
		sounds[2] = getAudioClip (getCodeBase(), "images/whistldn.wav");	//wrong
		sounds[3] = getAudioClip (getCodeBase(), "images/fio-trumpet.wav");	//right

		//the game instructions
		instructions = "Hello, and welcome to Bugs Gone Wild!";

		//show the instructions
		JOptionPane.showMessageDialog(null, instructions, "alert", JOptionPane.INFORMATION_MESSAGE);

		repaint();
		validate();

	}

	/** Description of paint(Graphics g)
	 *
	 * Function draws table and sets the table color
	 * @param g	graphics object used to draw table
	 * @return	void
	 */
  	public void paint(Graphics g)
  	{
  		g.setColor(Color.black);
   		aTable.draw(g);
   		validate();
  	}


	/** Description of mousePressed(MouseEvent e)
	*
	* Function listens for mouse events
	* @param e	Mouse event listener
	* @return	void
	*/
	public void mousePressed (MouseEvent e)
  	{
    		if(!guessMode){
  	  		if ((allowUserInput)){
  	    			aTable.swapSquareValue(e.getX(), e.getY());
   	    			repaint();
   	  		}
    		}
    		else{
      			System.out.println("guessed row = " + e.getY() + " guessed col = " + e.getX());
      			if(aTable.checkGuess(e.getX(), e.getY())){
				sounds[3].play();
        			int n = JOptionPane.showConfirmDialog(null, "Excellent!! Would you like to progress to next level?",
                                              "Correct!!!", JOptionPane.YES_NO_OPTION);
        			if (n == JOptionPane.YES_OPTION) {
          				guessMode = false;
          				this.add(BtnStart);
          				validate();
          				//size++;
        			}
        		else{
          			JOptionPane.showMessageDialog(null, "Thanks for playing!", "alert", JOptionPane.INFORMATION_MESSAGE);
        		}
      		}
      		else{
        		if(counter > 2)
          			counter = 0;
        		sounds[counter].play();
        		JOptionPane.showMessageDialog(null, "Nope");
        		counter++;
      		}

      		//repaint();
    	}

  }

  //set of mouse event listeners
  public void mouseClicked (MouseEvent e) {}
  public void mouseEntered (MouseEvent e) {}
  public void mouseReleased (MouseEvent e) {}
  public void mouseExited (MouseEvent e) {}

	/** Description of actionPerformed(e)
	*
	* Function 	listens for mouse actions
	* @param e	Button action listener
	* @return	void
	*/
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == enterCordBtn){
      	this.remove(enterCordBtn);
		  	this.add(BtnStart);
		  	validate();
		  	repaint();
    }

		allowUserInput = true;
		System.out.println(aTable.toString());
	}

}


